package com.gy.api.tv.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.gy.api.base.ApiIndex;
import com.gy.api.base.R;
import com.gy.api.base.RsaKeyProperties;
import com.gy.api.base.Type;
import com.gy.api.mobile.company.domain.vo.CompanyLoginResVO;
import com.gy.api.mobile.company.domain.vo.CompanyLoginVO;
import com.gy.api.mobile.company.domain.vo.CompanyVO;
import com.gy.api.mobile.company.service.ICompanyService;
import com.gy.api.mobile.user.domain.vo.UserLoginResVO;
import com.gy.api.mobile.user.domain.vo.UserLoginVO;
import com.gy.api.mobile.user.domain.vo.UserVO;
import com.gy.api.mobile.user.service.IUserService;
import com.gy.api.pc.sys.user.service.ISysUserService;
import com.gy.api.tv.domain.vo.SysUserTvOneVO;
import com.gy.api.tv.domain.vo.UserTvLoginVO;
import com.gy.api.tv.domain.vo.UserTvOneVO;
import com.gy.exception.BusinessException;
import com.gy.util.RsaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PrivateKey;

@RestController
@RequestMapping("/")
@Api( tags = ApiIndex.HuashuUserInfoController)
public class HuashuUserInfoController {

    @Autowired
    private ISysUserService service;

    @Autowired
    private IUserService userService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private RsaKeyProperties rsaKeyProperties;


    @ApiOperation("系统用户登录_密码登录[密码需要加密]")
    @PostMapping("login")
    public R<UserLoginResVO> login_password(@RequestBody UserTvLoginVO userLoginVO){
        UserLoginResVO resVO=service.loginTv(userLoginVO);
        return new R(resVO);
    }

    @ApiOperation("前端用户登录_密码登录[密码需要加密]")
    @PostMapping("h5_login")
    public R<UserLoginResVO> h5_login_password(@RequestBody UserTvLoginVO userLoginVO){
        try{
            com.gy.api.mobile.user.domain.vo.UserLoginVO userLoginVO1=new com.gy.api.mobile.user.domain.vo.UserLoginVO();
            PrivateKey privateKey = rsaKeyProperties.getPrivateKey();
            userLoginVO1.setPhone(RsaUtils.decrypt(userLoginVO.getUsername(), privateKey));
            userLoginVO1.setPassword(userLoginVO.getPassword());
            UserLoginResVO resVO= userService.login_password(userLoginVO1);
            if(resVO!=null&&resVO.getToken()!=null){
                return new R(resVO);
            }
        }catch (Exception e){}
        try{
            CompanyLoginVO companyLoginVO=new CompanyLoginVO();
            companyLoginVO.setLoginName(userLoginVO.getUsername());
            companyLoginVO.setPassword(userLoginVO.getPassword());
            CompanyLoginResVO resVO= companyService.login(companyLoginVO);
            if(resVO!=null&&resVO.getToken()!=null){
                UserLoginResVO userLoginResVO=new UserLoginResVO();
                userLoginResVO.setToken(resVO.getToken());
                return new R(userLoginResVO);
            }
        }catch (Exception e){}
        throw new BusinessException(Type.FAIL.getCode(),"账号密码错误！");
    }

    @ApiOperation("获取前端用户信息")
    @GetMapping("get_h5_info")
    @SaCheckLogin
    public R<UserTvOneVO> get_h5_info(){
        UserTvOneVO userTvOneVO=new UserTvOneVO();
        try{
            CompanyVO userVO=companyService.getCompanyOne();
            if(userVO!=null){
                userTvOneVO.setC_user(userVO);
                userTvOneVO.setType(1);
                return new R(userTvOneVO);
            }
        }catch (Exception e){
        }
        try{
            UserVO userVO=userService.getOne();
            if(userVO!=null){
                userTvOneVO.setUser(userVO);
                userTvOneVO.setType(2);
                return new R(userTvOneVO);
            }
        }catch (Exception e){
        }
        throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
    }

    @ApiOperation("获取系统用户信息")
    @GetMapping("getInfo")
    @SaCheckLogin
    public R<SysUserTvOneVO> getInfo(){
        SysUserTvOneVO resVO=service.getTvInfo();
        return new R(resVO);
    }



}
